package org.BUPT.domain.model.question;

import lombok.AccessLevel;
import lombok.Data;
import lombok.Getter;
import org.BUPT.domain.factory.RepoFactory;
import org.BUPT.infrastructure.exception.NullObjectException;
import org.BUPT.infrastructure.exception.PersistException;
import org.BUPT.infrastructure.persistent.po.Answer;
import org.BUPT.infrastructure.persistent.po.Question;
import org.BUPT.infrastructure.persistent.po.Question_Tag;
import org.BUPT.infrastructure.persistent.po.Question_TagPrimaryKey;
import org.BUPT.domain.service.impl.QuestionServiceImpl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Data
public class QuestionBO {

    public QuestionBO() throws PersistException {
    }
    public QuestionBO(Question question) throws PersistException {
        List<Answer> answers = questionServiceImpl.getAnswersOfQuestion(question.getId());
        List<String> tags = questionServiceImpl.getTagsOfQuestion(question.getId());
        List<AnswerBO> answerBOS = new ArrayList<>();

        for (Answer answer : answers) {
            AnswerBO answerBO = new AnswerBO(answer);
            answerBOS.add(answerBO);
        }
        List<Integer> subscribedUserIds = questionServiceImpl.getSubscribedUserOfQuestion(question.getId());
        this
                .setId(question.getId())
                .setUserId(question.getUserId())
                .setSubjectId(question.getSubjectId())
                .setTitle(question.getQuestionName())
                .setContent(question.getQuestionContent())
                .setCreateTime(question.getCreateTime())
                .setEditTime(question.getUpdateTime())
                .setSubscribedUsers(subscribedUserIds)
                .setAnswers(answerBOS)
                .setTags(tags)
                .setAnonymous(question.isAnonymous())
                ;

    }

    @Getter(AccessLevel.NONE)
    private final QuestionServiceImpl questionServiceImpl = RepoFactory.get(QuestionServiceImpl.class);
    private int id;
    private int userId;
    private int subjectId;
    private String title;
    private String content;
    private Date createTime;
    private Date editTime;
    private List<Integer> subscribedUsers;
    private List<AnswerBO> answers;
    private List<String> tags;
    private int viewNum;
    private boolean isAnonymous;



    public void save() throws NullObjectException, PersistException {

        Question question = RepoFactory.get(QuestionServiceImpl.class).saveQuestion(
                new Question()
                        .setQuestionName(getTitle())
                        .setQuestionContent(getContent())
                        .setUserId(getUserId())
                        .setSubjectId(getSubjectId())
                        .setCreateTime(getCreateTime())
                        .setUpdateTime(getEditTime())
                        .setIsDel(0)
                        .setViewNum(getViewNum())
                        .setTimestamp(getEditTime()!=null?getEditTime():getCreateTime())
                        .setAnonymous(isAnonymous())
        );

        for (String tag :getTags()){
            Question_Tag question_tag = new Question_Tag();
            Question_TagPrimaryKey question_tagPrimaryKey = new Question_TagPrimaryKey();
            question_tagPrimaryKey.setQuestionId(question.getId())
                    .setTagName(tag);
            question_tag.setQuestion_tagPrimaryKey(question_tagPrimaryKey);
            questionServiceImpl.addQuestionTag(question_tag);
        }
    }
    public void delete(){
        // todo: 删除问题
    }

    public QuestionBO view() throws PersistException {
        // 生成一个缩略信息，减少网络负载
        // （注意，这个缩略信息中未注入questionBOFactory，所以不可以持久化）
        return new QuestionBO()
                .setId(getId())
                .setTitle(getTitle())
                .setSubjectId(getSubjectId())
                .setCreateTime(getCreateTime())
                .setEditTime(getEditTime())
                .setUserId(getUserId());
    }


}

